package com.KKd.controller;

import com.KKd.pojo.Blog;
import com.KKd.pojo.Catalogue;
import com.KKd.service.BlogServiceImpl;
import com.KKd.service.CatalogueServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 14587
 * @date 2020/11/14 16:37
 */
@RequestMapping("/cata")
@Controller
public class CatalogueController {

    @Autowired
    private CatalogueServiceImpl catalogueService;
    @Autowired
    private BlogServiceImpl blogService;
    //给博客添加分类
    //去分类页
    //这里分两种情况，一种是带参数的，这个时候我们要执行的是添加操作，也就是有添加的键，
    //没有id的话，就只有创建分类和删除分类的键，这里猛然想到还需要一个删除分类的操作
    @RequestMapping("/toCata")
    public String toCataPage( Integer blogId, Model model){
        //这里我们先要把分类的信息返回
        List<Catalogue> catalogues = catalogueService.showCatalogue();
        //这块不能直接把分类名返回，因为我们还要利用分类的id进行删除
        //List<String> cataNameList = new ArrayList<String>();
        if (blogId!=null){
            model.addAttribute("blogId",blogId);
        }
        model.addAttribute("catalogues",catalogues);
        return "catalogue";
    }
    //接受分类管理页中的数据，并且返回json字符串
    @PostMapping("/admin/showCata")
    @ResponseBody
    public List<String> showCataBlogs(Integer catalogueId){
        List<String> blogTitles = catalogueService.showBlogTitleByCata(catalogueId);
        return blogTitles;
    }
    //接收首页中的数据，并且返回字符串
    //这里的性能确实不够高
    @PostMapping("/client/showCata")
    @ResponseBody
    public List<String> showCataIndex(String catalogueName){
        Integer catalogueId=null;
        List<Catalogue> catalogues = catalogueService.showCatalogue();
        for (Catalogue catalogue : catalogues) {
            if (catalogue.getCatalogueName().equals(catalogueName)){
                catalogueId=catalogue.getCatalogueId();
            }
        }
        List<String> blogTitles = catalogueService.showBlogTitleByCata(catalogueId);
        return blogTitles;
    }
    //添加分类，传入博客的id和分类的id
    @RequestMapping("/addCata/{blogId}/{catalogueId}")
    public String addCata(@PathVariable Integer blogId,@PathVariable Integer catalogueId){
        //这里我们需要先判断是否这个分类中已经有了这个博客，所以需要先查找判断一下
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("blogId", blogId);
        String blogTitleRes = blogService.queryBlog(map).getTitle();
        boolean isContained = catalogueService.showBlogTitleByCata(catalogueId).contains(blogTitleRes);
        //这里不包括才需要添加
        if (!isContained){
            String catalogueName = catalogueService.getCataNameByCataId(catalogueId);
            catalogueService.insertCatalogueById(new Catalogue(null,catalogueId,blogId,catalogueName));
        }
        return "redirect:/cata/toCata";
    }
    //创建分类
    @RequestMapping("/createCata")
    public String CreateCata(String catalogueName){
        //这里先查询存在不存在
        boolean isContained = catalogueService.showCatalogue().contains(catalogueName);
        if (!isContained){
            //创建分类
            //这里我们需要一个静态变量记录分类的id,每次创建分类的时候，分类id传这个
             Integer catalogueCount=catalogueService.getBiggestCataId();
             //这里总是会报空值异常，原因就是查询不出来的时候，也就是没有分类的时候为null
            //这个时候我们还是特殊处理一下
             if (catalogueCount==null){
                 catalogueCount=0;
             }
            catalogueService.insertCatalogue(new Catalogue(null,++catalogueCount,null,catalogueName));
        }
        return "redirect:/cata/toCata";
    }
    //取消加入分类
    @RequestMapping("/deleteBlogInCata/{title}")
    public String deleteBlogInCata(@PathVariable String title){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", title);
        Blog blog = blogService.queryBlog(map);
        Integer blogId=blog.getBlogId();
        catalogueService.deleteCatalogueById(blogId);
        return "redirect:/cata/toCata";
    }
    //删除分类
    @RequestMapping("/deleteCata/{catalogueId}")
    public String deleteCata(@PathVariable Integer catalogueId){
        catalogueService.deleteCatalogue(catalogueId);
        return "redirect:/cata/toCata";
    }
}
